/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Screen;

import ClassFrame.Button;
import ClassFrame.InputKey;
import ClassFrame.Map;
import ClassFrame.Resource;
import GamePlay.CanvasGame;
import javax.microedition.lcdui.Graphics;
/**
 *
 * @author QuyetNM1
 */
public class Welcome extends IScreen {
    private long startTime;
    private long currTime;
    
    private Button btnPlay, btnMenu;
    
    private int state = 0;

    public Welcome(CanvasGame _canvas) {
        super(_canvas);
        startTime = System.currentTimeMillis();
        currTime = startTime;
        
    }

    public void initComponents(){
        btnPlay = new Button("Sẵn sàng", Resource.IMG_BTN_LT, 5, 285);
        btnMenu = new Button("Chưa", Resource.IMG_BTN_LT, 155, 285);
    }
    
    public void load_screen() {
        canvas.getResource().loadArray(Resource.welcomeArrayImages);
    }

    public void un_load_screen() {
        canvas.getResource().unLoadArray(Resource.welcomeArrayImages);
    }

    public void keyEvent(int keycode, int event) {
        if(event == InputKey.KEY_EVENT_DOWN){ 
            state++;
            if(keycode == InputKey.KEY_LEFT_SOFTKEY){
                canvas.setScreen( new ChooseLevel(canvas) );
            } else if(keycode == InputKey.KEY_RIGHT_SOFTKEY){
                canvas.setScreen( new Menu(canvas) );
            }
        }
    }

    public void pointerEvent(int x, int y, int event) {
        switch(event){
             case InputKey.POINTER_EVENT_DOWN:
                 state++;
                 if(canvas.checkButton(x, y, btnPlay)){
                    btnPlay.setState(Button.HOLD_CLICK);
                    canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                 } else if(canvas.checkButton(x, y, btnMenu)){
                    btnMenu.setState(Button.HOLD_CLICK);
                    canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                 }
                 break;
             case InputKey.POINTER_EVENT_UP:
                 if(canvas.checkButton(x, y, btnPlay) && btnPlay.getState() == Button.HOLD_CLICK){
                     canvas.getRMS().open();
                     Map.levelMax = canvas.getRMS().readLevelMax();
                     System.out.println("level max " + Map.levelMax);
                     canvas.getRMS().close();
                     canvas.setScreen( new ChooseLevel(canvas) );
                 } else if(canvas.checkButton(x, y, btnMenu) && btnMenu.getState() == Button.HOLD_CLICK){
                     canvas.setScreen( new Menu(canvas) );
                 }
                 btnPlay.setState(Button.ENABLE);
                 btnMenu.setState(Button.ENABLE);
                 break;
         }
    }

    public void update() {
    }
    
    private int count = 0;
    
    public void paint(Graphics g) {
        count++;
        switch(state){
            case 0:
                canvas.getImage().drawImage(g, canvas, Resource.IMG_WELCOME1, 0, 0);
                if(count %16 < 8){
                    canvas.getImage().drawImage(g, canvas, Resource.IMG_CONTINUE, 0, 260);
                }
                break;
            case 1:
                canvas.getImage().drawImage(g, canvas, Resource.IMG_WELCOME2, 0, 0);
                if(count %16 < 8){
                    canvas.getImage().drawImage(g, canvas, Resource.IMG_CONTINUE, 0, 260);
                }
                break;
            default:
                canvas.getImage().drawImage(g, canvas, Resource.IMG_WELCOME3, 0, 0);
                btnPlay.drawButton(g, canvas);
                btnMenu.drawButton(g, canvas);
                break;
        }
    }
}
